﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MDIApplication.Classes;

namespace MDIApplication
{
    public partial class Login : Form
    {
        delegate void SetTextCallback(string text);
        public Thread oThread;

        public Login()
        {
            InitializeComponent();
        }

        private void Login_Shown()
        {
            while (true)
            {
                this.SetText(".");
            }
        }

        private void Login_Load(object sender, EventArgs e)
        {
            label2.Visible = false;
        }
       
        private void btnLogin_Click(object sender, EventArgs e)
        {
            label2.Visible = true;
            oThread = new Thread(new ThreadStart(this.Login_Shown));
            oThread.IsBackground = true;
            oThread.Start();

            //Login Authentication
            SqlConnection conDataBase = new SqlConnection();
            conDataBase.ConnectionString = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
            SqlCommand cmdDataBase = new SqlCommand("SELECT Staff_Account_ID,Password FROM Staff WHERE Staff_Account_ID  ='" + txtId.Text + "'", conDataBase); ;
            conDataBase.Open();
            SqlDataReader dataRead = cmdDataBase.ExecuteReader();
            dataRead.Read();
            if (dataRead.HasRows)
            {
                String id = (string)dataRead["Staff_Account_ID"];
                String pwd = (string)dataRead["Password"];
                if (pwd.Equals(txtPassword.Text))
                {
                    //Staff Authorzation
                    if (AccountDBManager.AuthorizedByStaffIdAndPwd(id,pwd)=="Administrator")
                    {
                        oThread.Abort();                    
                        Form1 f1 = new Form1("Administrator");                       
                        f1.Activate();
                        f1.Show();                       
                        this.Hide();                       
                    }
                    else if (AccountDBManager.AuthorizedByStaffIdAndPwd(id, pwd) == "Manager")
                    {
                        oThread.Abort();
                        Form1 f1 = new Form1("Manager");
                        f1.Activate();
                        f1.Show();
                        this.Hide();
                    }
                    else if (AccountDBManager.AuthorizedByStaffIdAndPwd(id, pwd) == "FirstLine")
                    {
                        oThread.Abort();
                        Form1 f1 = new Form1("FirstLine");
                        f1.Activate();
                        f1.Show();
                        this.Hide();
                    }

                }
                if (!pwd.Equals(txtPassword.Text))
                {
                    oThread.Abort();              
                    MessageBox.Show("Incorrect Password");
                    label2.Visible = false;           
                }
            }
            else
            {
                oThread.Abort();
                MessageBox.Show("Incorrect Staff ID");
                label2.Visible = false;  
            }
            dataRead.Close();
            conDataBase.Close();
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void SetText(string text)
        {           
            if (this.label2.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                if (this.label2.Text.Length == 16)
                    this.label2.Text = "Log in";
                else
                {
                    this.label2.Text += text;
                }
            }
        }



    }
}
